import Vue from 'vue';

const state = {
	cartList: [{
			"isSelected": true,
			"count": 1,
			"promId": 0,
			"showPoints": false,
			"itemTagList": [{
				"itemId": 1535004,
				"tagId": 128111157,
				"freshmanExclusive": false,
				"name": "暖冬特惠",
				"subType": 204,
				"forbidJump": false,
				"type": 2
			}],
			"rank": 1,
			"id": 1535004,
			"sellVolume": 4001,
			"primaryPicUrl": "https://yanxuan-item.nosdn.127.net/f79906f1b1fe86420ea40473de66ec0e.png",
			"soldOut": false,
			"sortFlag": 0,
			"commentCount": 0,
			"onSaleTime": 1538101761748,
			"picMode": 1,
			"commentWithPicCount": 0,
			"underShelf": false,
			"status": 2,
			"couponConflict": true,
			"forbiddenBuy": false,
			"promotionDesc": "暖冬特惠",
			"limitedFlag": 204,
			"pieceNum": 0,
			"itemSizeTableDetailFlag": false,
			"forbidExclusiveCal": false,
			"rewardShareFlag": false,
			"updateTime": 1575893634989,
			"showCommentEntrance": true,
			"pieceUnitDesc": "件",
			"specialPromTag": "",
			"counterPrice": 299,
			"categoryL2Id": 0,
			"retailPrice": 209,
			"primarySkuPreSellPrice": 0,
			"preLimitFlag": 0,
			"itemPromValid": true,
			"promTag": "暖冬特惠",
			"source": 0,
			"points": 0,
			"primarySkuPreSellStatus": 0,
			"extraServiceFlag": 0,
			"flashPageLink": "",
			"autoOnsaleTimeLeft": 0,
			"innerData": {},
			"saleCenterSkuId": 0,
			"pointsStatus": 0,
			"extraPrice": "",
			"colorNum": 0,
			"showTime": 0,
			"autoOnsaleTime": 0,
			"preemptionStatus": 1,
			"isPreemption": 0,
			"zcSearchFlag": false,
			"name": "男式色拉姆内衣套装2.0",
			"appExclusiveFlag": false,
			"itemType": 1,
			"listPicUrl": "https://yanxuan-item.nosdn.127.net/c2eeb1b872af1b8efc179a7515aacdaa.png",
			"pointsPrice": 0,
			"simpleDesc": "色拉姆发热面料，加厚升级",
			"seoTitle": "",
			"newItemFlag": false,
			"buttonType": 0,
			"primarySkuId": 1636062,
			"displaySkuId": 1636056,
			"productPlace": "",
			"itemSizeTableFlag": false
		},
		{
			"isSelected": true,
			"count": 1,
			"promId": 0,
			"showPoints": false,
			"itemTagList": [{
				"itemId": 1536001,
				"tagId": 128111157,
				"freshmanExclusive": false,
				"name": "暖冬特惠",
				"subType": 204,
				"forbidJump": false,
				"type": 2
			}],
			"rank": 1,
			"id": 1536001,
			"sellVolume": 3634,
			"primaryPicUrl": "https://yanxuan-item.nosdn.127.net/32b8b2d07b1c4327593a4a70993eeac2.png",
			"soldOut": false,
			"sortFlag": 0,
			"commentCount": 0,
			"onSaleTime": 1538101896296,
			"picMode": 1,
			"commentWithPicCount": 0,
			"underShelf": false,
			"status": 2,
			"couponConflict": true,
			"forbiddenBuy": false,
			"promotionDesc": "暖冬特惠",
			"limitedFlag": 204,
			"pieceNum": 0,
			"itemSizeTableDetailFlag": false,
			"forbidExclusiveCal": false,
			"rewardShareFlag": false,
			"updateTime": 1575894115275,
			"showCommentEntrance": true,
			"pieceUnitDesc": "件",
			"specialPromTag": "",
			"counterPrice": 299,
			"categoryL2Id": 0,
			"retailPrice": 209,
			"primarySkuPreSellPrice": 0,
			"preLimitFlag": 0,
			"itemPromValid": true,
			"promTag": "暖冬特惠",
			"source": 0,
			"points": 0,
			"primarySkuPreSellStatus": 0,
			"extraServiceFlag": 0,
			"flashPageLink": "",
			"autoOnsaleTimeLeft": 0,
			"innerData": {},
			"saleCenterSkuId": 0,
			"pointsStatus": 0,
			"extraPrice": "",
			"colorNum": 0,
			"showTime": 0,
			"autoOnsaleTime": 0,
			"preemptionStatus": 1,
			"isPreemption": 0,
			"zcSearchFlag": false,
			"name": "女式色拉姆内衣套装2.0",
			"appExclusiveFlag": false,
			"itemType": 1,
			"listPicUrl": "https://yanxuan-item.nosdn.127.net/02b61fb5700aed6761b7524d98ed0837.png",
			"pointsPrice": 0,
			"simpleDesc": "色拉姆发热面料，加厚升级",
			"seoTitle": "",
			"newItemFlag": false,
			"buttonType": 0,
			"primarySkuId": 1634105,
			"displaySkuId": 1634104,
			"productPlace": "",
			"itemSizeTableFlag": false
		},

	],
	total: 0
}

const mutations = {
	// 添加购物车 --- 考虑商品重复问题
	addGoodsMutations(state, goods) {
		const exist = state.cartList.find(item => (item.id === goods.id));
		if (exist) {
			// 购物车已经存在当前的商品
			exist.count++;
			exist.retailPrice = exist.retailPrice + goods.retailPrice;
		} else {
			// 购物车不存在当前的商品

			// 使用下面的语句数据是非响应式的，即使数据变了 页面也不会发生改变
			// goods.count = 1;
			// goods.isSelected = true;

			// 使用Vue.set将数据响应式
			Vue.set(goods, 'count', 1);
			Vue.set(goods, 'isSelected', true);
			state.cartList.push(goods);
		}

		wx.showToast({
			title: '添加购物车成功',
			icon: 'success', // 可以是 'success'、'loading'、'none'
			duration: 2000 // 持续时间，单位毫秒，默认1500
		});
	},

	// 添加或者减少商品数量
	changeGoodsCountMutations(state, {
		change,
		index
	}) {
		// 增加数量
		if (change) {
			state.cartList[index].count++
		} else {
			if (state.cartList[index].count === 1) {
				wx.showModal({
					title: '提示',
					content: '一个都不要了么?',
					success(res) {
						if (res.confirm) {
							state.cartList.splice(index, 1);
						} else if (res.cancel) {}
					}
				});
				// wx.showToast({
				// 	title: '就剩一件啦~',
				// 	icon: 'error', // 可以是 'success'、'loading'、'none'
				// 	duration: 2000 // 持续时间，单位毫秒，默认1500
				// });
			} else {
				state.cartList[index].count--;
			}
		}
	},

	// 修改购物车单个商品选中状态
	changeStateMutations(state, {
		isSelected,
		index
	}) {
		state.cartList[index].isSelected = isSelected
	},

	// 修改全选按钮选中状态 进而控制购物车商品的选中状态
	changeAllStateMutations(state, isSelected) {
		state.cartList.forEach(item => item.isSelected = isSelected)
	}
}

const actions = {

}

const getters = {
	// 反选功能
	isAllSelectedGetters(state) {
		// 全为真 则为真      一个假 则为假
		return state.cartList.every(item => item.isSelected === true)
	},

	// 价格总和
	allPriceGetters(state) {
		state.total = 0;
		for (let i = 0; i < state.cartList.length; i++) {
			if (state.cartList[i].isSelected) {
				state.total = state.total + state.cartList[i].retailPrice * state.cartList[i].count
			}
		}
		return state.total
	},

	// 选中商品项的个数
	selectedGoodsNumGetters(state) {
		const num = state.cartList.filter(item => item.isSelected)
		return num.length
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions,
	getters
}